package cn.neo.hhrpc.provider;

import cn.neo.hhrpc.api.User;
import cn.neo.hhrpc.api.UserService;
import cn.neo.hhrpc.core.annotation.HHProvider;
import cn.neo.hhrpc.core.api.RpcContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 需要将该服务暴露，使得远程服务可以访问该类的方法
 */

@Component
// 标记该类为provider
@HHProvider
public class UserServiceImpl implements UserService {


    @Autowired
    Environment environment;

    @Override
    public User findById(int id) {
        return new User(id, "HH-V1-"
                + environment.getProperty("server.port")
                + "-" + System.currentTimeMillis());
    }

    @Override
    public User findById(int id, String name) {
        return new User(id, "HH-" + name + "_" + System.currentTimeMillis());
    }

    @Override
    public long getId(long id) {
        return id;
    }

    @Override
    public long getId(User user) {
        return user.getId().longValue();
    }

    @Override
    public long getId(float id) {
        return 1L;
    }

    @Override
    public String getName() {
        return "HH123";
    }

    @Override
    public String getName(int id) {
        return "Cola-" + id;
    }

    @Override
    public int[] getIds() {
        return new int[]{100, 200, 300};
    }

    @Override
    public long[] getLongIds() {
        return new long[]{1, 2, 3};
    }

    @Override
    public int[] getIds(int[] ids) {
        return ids;
    }

    @Override
    public List<User> getList(List<User> userList) {
        return userList;
    }

    @Override
    public Map<String, User> getMap(Map<String, User> userMap) {
        return userMap;
    }

    @Override
    public Boolean getFlag(boolean flag) {
        return !flag;
    }

    @Override
    public User[] findUsers(User[] users) {
        return users;
    }

    @Override
    public User findById(long id) {
        return new User(Long.valueOf(id).intValue(), "HH");
    }

    @Override
    public User ex(boolean flag) {
        if (flag) throw new RuntimeException("just throw an exception");
        return new User(100, "HH100");
    }

    @Override
    public String find(int timeout, String port1) {
        String port = environment.getProperty("server.port");
        if (port1.equals(port) || "8094".equals(port)) {
            try {
                Thread.sleep(timeout);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        return "设置成功。端口" + port1 + "超时时间" + timeout;
    }

    @Override
    public User find(int timeout) {
        String port = environment.getProperty("server.port");
        try {
            Thread.sleep(timeout);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return new User(1001, "HH1001-" + port);
    }


    @Override
    public String echoParameter(String key) {
        System.out.println("=========>> RpcContext.ContextParameters: ");
        RpcContext.ContextParameters.get().forEach((x, y) -> System.out.println(x + "-->" + y));
        return RpcContext.getContextParameters(key);
    }
}
